package fr.business.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import fr.business.ManageApplicationsBusinessService;
import fr.business.ManageParticipationsBusinessService;
import fr.form.Application;
import fr.form.Participation;
import fr.form.synthese.SyntheseParticipation;
import fr.persistence.ManageParticipationPersistenceService;
import fr.utilities.Constantes.StatutParticipation;
import fr.utilities.DateUtil;

@Service
@Transactional
public class ManageParticipationsBusinessServiceImpl 
implements ManageParticipationsBusinessService{


	@Autowired
	private ManageParticipationPersistenceService psParticipation;
	   
	@Autowired
   private ManageApplicationsBusinessService psApp;

	public List<SyntheseParticipation> readParticipationsByParam(List<Integer> pStatutParticipationList,Integer pUserId,Integer pApplicationId) {
	
		List<Participation> participations = psParticipation.readByParam(pStatutParticipationList,pUserId,pApplicationId);
		
		List<SyntheseParticipation> synParticipations = new ArrayList<SyntheseParticipation>();
		for(Participation participation :participations){
			synParticipations.add(participationToSynthese(participation));
		}
		
		Collections.sort(synParticipations);
		
		return synParticipations;
		
		
	}

	public SyntheseParticipation participationToSynthese(Participation participation){

		String etoile = " * ";
		
		SyntheseParticipation syn = new SyntheseParticipation();

		syn.setId(participation.getId());
		syn.setApplication(participation.getApplication().getLibelle());
		syn.setUser(participation.getUser().getNom()+" "+participation.getUser().getPrenom());
		syn.setDateMaj(participation.getDateMaj());
		syn.setDateCreate(participation.getDateCreate());
		syn.setDateMajStr(DateUtil.getDateView(participation.getDateCreate()));
		
		syn.setStatutParticipation(participation.getStatutParticipationObj().getLibelle());
		
		StringBuffer resps = new StringBuffer();
		resps.append(participation.getReponse()).append(etoile);
		resps.append(participation.getReponse1()).append(etoile);
		resps.append(participation.getReponse2()).append(etoile);
		resps.append(participation.getReponse3()).append(etoile);
		resps.append(participation.getReponse4()).append(etoile);
		resps.append(participation.getReponse5()).append(etoile);
		resps.append(participation.getReponse6()).append(etoile);
		resps.append(participation.getReponse7());

		syn.setReponses(resps.toString());
		
		return syn;

	}

	public String validerParticipation(Integer id) {
		
		Participation participation = psParticipation.readObject(id);
		
		if(participation.getStatutParticipation().equals(StatutParticipation.COMMENCEE.getCode())){
			return "erreur1";
		}else if(participation.getStatutParticipation().equals(StatutParticipation.VALIDEE_SU.getCode())){
			return "erreur2";
		}else{
			
			Application app =  psApp.readObject(participation.getIdApplication());
			if(!app.getReponse().equals(participation.getReponse())){
				return "erreur3";
			}else{
				participation.setStatutParticipation(StatutParticipation.VALIDEE_SU.getCode());
				participation.setDateMaj(new Date());
				psParticipation.updateObject(participation);
				return "ok";
			}
		}
		
	}

	public String refuserParticipation(Integer id) {
		
		Participation participation = psParticipation.readObject(id);
		
		if(participation.getStatutParticipation().equals(StatutParticipation.COMMENCEE.getCode())){
			return "erreur1";
		}else if(participation.getStatutParticipation().equals(StatutParticipation.NON_VALIDEE_SU.getCode())){
			return "erreur2";
		}else{
			participation.setStatutParticipation(StatutParticipation.NON_VALIDEE_SU.getCode());
			participation.setDateMaj(new Date());
			psParticipation.updateObject(participation);
			return "ok";
		}
	
	}

}
